package com.zhiyun.service.impl;

import com.zhiyun.domain.*;
import com.zhiyun.mapper.CourseMapper;
import com.zhiyun.mapper.SelectCourseMapper;
import com.zhiyun.mapper.StudentMapper;
import com.zhiyun.mapper.TeacherMapper;
import com.zhiyun.service.CourseService;
import com.zhiyun.util.Db;
import com.zhiyun.vo.Result;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author zc
 * @Date 2025/4/29 上午10:40
 */
public class CourseServiceImpl implements CourseService {
    private StudentMapper studentMapper = Db.getMapper(StudentMapper.class);
    private CourseMapper courseMapper = Db.getMapper(CourseMapper.class);
    private SelectCourseMapper selectCourseMapper = Db.getMapper(SelectCourseMapper.class);
    private TeacherMapper teacherMapper = Db.getMapper(TeacherMapper.class);
    @Override
    public Result stuCourseList(int stuId) {
        Student student = studentMapper.selectByStuId(stuId);
        List<Course> list = student.getCourseList();
        return Result.ok(list);
    }

    @Override
    public Result addCourse(int studentId,String inviteCode) {
        Course course = courseMapper.selectByInviteCode(inviteCode);
        if (course == null){
            return Result.fail("邀请码不存在");
        }
        SelectCourse selectCourse = new SelectCourse();
        selectCourse.setCourseId(course.getId());
        selectCourse.setStudentId(studentId);
        int rows = selectCourseMapper.insert(selectCourse);
        if (rows > 0){
            return Result.okMsg("添加成功");
        }else {
            return Result.fail("添加失败");
        }
    }

    @Override
    public Result exitCourse(int stuId, Integer courseId) {
        int rows = selectCourseMapper.exitCourse(stuId,courseId);
        if (rows > 0){
            return Result.okMsg("退课成功");
        }else {
            return Result.fail("退课失败");
        }
    }

    @Override
    public Result findById(int courseId) {
        return Result.ok(courseMapper.selectByPrimaryKey(courseId));
    }

    @Override
    public Result teacherCourseList(int teacherId) {
        Teacher teacher = teacherMapper.selectByTeacherId(teacherId);
        List<Course> list = teacher.getCourses();
        return Result.ok(list);
    }


    @Override
    public Result insertCourse(Course course) {
       Course oldCourse =  courseMapper.selectByName(course.getName());
       if (oldCourse != null){
           return Result.fail("已经有该门课程了");
       }
        char[] chars = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890".toCharArray();
        Random random = new Random();
        StringBuffer inviteCodeStr = new StringBuffer();
        String inviteCode;
        while (true){
            for (int i = 0; i < 6; i++) {
                inviteCodeStr.append(chars[random.nextInt(chars.length)]);
            }
            inviteCode = inviteCodeStr.toString();
            //如果没有课程用该验证码就跳出循环,不然就重复生成六位验证码
            Course oldCourse1 = courseMapper.selectByInviteCode(inviteCode);
            if (oldCourse1 == null){
                break;
            }
            inviteCodeStr = new StringBuffer();
        }
        course.setInviteCode(inviteCode);
        int rows = courseMapper.insert(course);
        if (rows > 0){
            return Result.ok(inviteCode);
        }
        return Result.fail("添加失败");
    }

    @Override
    public Result AllStudent(Integer courseId) {
        List<Student> students = selectCourseMapper.selectAllStudent(courseId);
        return Result.ok(students);
    }

    @Override
    public Result deleteStudent(int courseId, Integer studentId) {
        int rows = selectCourseMapper.deleteByCourseIdAndStudentId(courseId,studentId);
        if (rows > 0){
            return Result.okMsg("将该学生移除课程成功");
        }
        return Result.fail("将该学生移除课程失败");
    }


}
